﻿using System;
using System.Collections.Generic;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.Dao
{
    public class CramDailyActivityRepository
    {

        public static IEnumerable<Cram_DailyActivity> GetAll()
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_DailyActivities.ToList();
            }
        }

        public static IEnumerable<Cram_DailyActivity> GetByCramLineId(Guid cramLineId)
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_DailyActivities.Where(al => al.CramLineId == cramLineId);
            }
        }

        public static void Create(Cram_DailyActivity activity)
        {
            using (var context = new eCramDataContext())
            {
                context.Cram_DailyActivities.InsertOnSubmit(activity);
                context.SubmitChanges();
            }
        }

        public static void InsertActivities(IEnumerable<Cram_DailyActivity> activities)
        {
            using (var context = new eCramDataContext())
            {
                foreach (var cramDailyActivity in activities)
                {
                    context.Cram_DailyActivities.InsertOnSubmit(cramDailyActivity);
                }
                context.SubmitChanges();
            }
        }

        public static void Delete(Cram_DailyActivity activity)
        {
            using (var context = new eCramDataContext())
            {
                Cram_DailyActivity originalCramLine =
                    context.Cram_DailyActivities.FirstOrDefault(al => al.Id == activity.Id);
                if (originalCramLine != null)
                {
                    context.Cram_DailyActivities.DeleteOnSubmit(originalCramLine);
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteActivities(IQueryable<Cram_DailyActivity> activities)
        {
            using (var context = new eCramDataContext())
            {
                foreach (var cramDailyActivity in activities)
                {
                    context.Cram_DailyActivities.DeleteOnSubmit(cramDailyActivity);
                }
                context.SubmitChanges();
            }
        }

        public static void DeleteActivities(Guid cramLineId)
        {
            using (var context = new eCramDataContext())
            {

                foreach (var cramDailyActivity in context.Cram_DailyActivities.Where(al => al.CramLineId == cramLineId))
                {
                    context.Cram_DailyActivities.DeleteOnSubmit(cramDailyActivity);
                }
                context.SubmitChanges();
            }
        }

        public static Cram_DailyActivity GetById(Guid activityId)
        {
            using (var context = new eCramDataContext())
            {
                return
                       context.Cram_DailyActivities.FirstOrDefault(p => p.Id == activityId);
            }
        }


    }
}
